// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/sparepartshospital/api/SpareStockRecordApi'

import ComClass from '@api/ComQuery'
import OneClass from '@api/sparepartshospital/model/SpareStockRecordListQueryReq'
import AddClass from '@api/sparepartshospital/model/SpareStockRecordOperateReq'
import DelClass from '@api/sparepartshospital/model/SpareStockRecordReq'
import SpareStockRecordDto from '@api/sparepartshospital/model/SpareStockRecordDto'
import SpareRecordDto from '@api/sparepartshospital/model/SpareRecordDto'
import SpareDto from '@api/sparepartshospital/model/SpareDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne(one, TIP = '查询库存详情') {
    const DTO = CORE.grpcDTO(OneClass, one, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'querySpareStockRecordSpareList', DTO)
    return { children: resp.content || [] }
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd(one) {
    return { ...one, recordCode: '' }
  },
  async add(one, TIP = '新增库存记录') {
    const children = [SpareRecordDto, 'content', 'children', (dto, obj) => {
      CORE.grpcDTO(dto, obj, [SpareDto, 'spare'])
    }]

    const DTO = new AddClass()
    const baseDTO = CORE.grpcDTO(SpareStockRecordDto, one, children)
    DTO.setContent(baseDTO)

    await CORE.grpc(TIP, CLIENT, 'insertSpareStockRecord', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  transDel(one) {
    return one.row.children[0]
  },
  async del({ id }, TIP = '删除库存记录') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteSpareStockRecordSpare', DTO)
    return CORE.grpcOK(TIP)
  },
}
